package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.coolapp.service.CoolAppHomeService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppHomeMenuDTO;
import com.xbongbong.pro.coolapp.pojo.vo.CoolAppHomeMenuVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhushuainan
 * @title: CoolAppHomeController
 * @projectName xbb-pro
 * @description: 酷应用首页
 * @date 2022/6/9 16:16
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/coolapp")
public class CoolAppHomeController {

    @Resource
    private CoolAppHomeService coolAppHomeService;

    /**
        * @description: 获取酷应用首页
        * @param coolAppHomeMenuDTO
        * @param br
        * @return java.lang.String
        * @author zhushuainan
        * @date 2022/06/10 10:24:38
        */
    @RequestMapping(value = "/index/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCoolAppHome(@RequestBody @Valid CoolAppHomeMenuDTO coolAppHomeMenuDTO, BindingResult br){
        XbbResponse<CoolAppHomeMenuVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CoolAppHomeMenuVO coolAppHomeMenuVO = coolAppHomeService.getIndex(coolAppHomeMenuDTO);
                response = new XbbResponse<>(coolAppHomeMenuVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}



